const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalShow: false,
    shopName: '',
    shopId: '',
    currency: {},
    type: '0',
    typeName: ['游戏币', '游戏券', '币券套餐'],
    detail: {},
    hintModelShow: false
  },

  closeModal() {
    this.setData({
      modalShow: false
    })
  },
  closeHintModal() {
    this.getSubscribeMessage()
    this.setData({
      hintModelShow: false
    })
  },
  buy() {
    wx.navigateTo({
      url: `/pages/currencyShop/submitOrder/index?shopName=${this.data.shopName}&currency=${encodeURIComponent(JSON.stringify(this.data.currency))}&shopId=${this.data.shopId}&type=${this.data.type}`
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let currency = {}
    let goodsKindType=""
    if (options.type == 1) {
      currency = JSON.parse(decodeURIComponent(options.currency))
      goodsKindType = currency.goodsKind
    } else if (options.type == 2) {
      let currencies = JSON.parse(decodeURIComponent(options.currency)).goodsCombo.filter(item => item.subGoodsKind == '02-0201' || item.subGoodsKind == '02-0202')
      if (currencies[0]) {
        currency = currencies[0]
        goodsKindType = currencies[0].subGoodsKind
      } else {
        currency = {}
      }
    }
    let detail = {}
    if (options.type != 0) {
      const ticketDetail = currency.subChannelGameCoupons
      const machineDetail = JSON.parse(ticketDetail.params)
      if (goodsKindType == '02-0201') { //券包
        detail.playLimit = '无限制'
        detail.machineDetail = machineDetail.map(item => `${item.machineName}/${item.count}局`).join('、')
      } else if (goodsKindType == '02-0202') { //次卡
        detail.playLimit = ticketDetail.dailyUseLimit ? `可玩${ticketDetail.dailyUseLimit}局` : '无限制'
        detail.machineDetail = machineDetail.map(item => item.machineName).join('、')
      }
      const enums = {
        '1': '天',
        '2': '周',
        '3': '月',
        '4': '年'
      }
      detail.buyLimit = (ticketDetail.limitUnit && ticketDetail.amountLimit) ? `每${enums[ticketDetail.limitUnit]}可购买${ticketDetail.amountLimit}张` : '无限制'
      if (ticketDetail.validType == 1) {
        const start = ticketDetail.validStart.split(' ')[0].split('-')
        const end = ticketDetail.validEnd.split(' ')[0].split('-')
        detail.time = `${start[0]}年${start[1]}月${start[2]}日至${end[0]}年${end[1]}月${end[2]}日`
      } else {
        const now = new Date()
        const after = new Date(now.getTime() + (ticketDetail.validDays - 1) * 24 * 60 * 60 * 1000)
        detail.time = `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日至${after.getFullYear()}年${after.getMonth() + 1}月${after.getDate()}日`
      }
    }
    this.setData({
      detail,
      shopName: options.shopName,
      shopId: options.shopId,
      type: options.type,
      currency: JSON.parse(decodeURIComponent(options.currency)),
      notBuy: !!options.notBuy,
      hintModelShow: options.entrance == 0 ? true : false //是否从首页进入
    })
    wx.setNavigationBarTitle({
      title: this.data.typeName[options.type] + '详情页'
    })
  },
  /**
   * 1.点击【我知道了】显示订阅消息弹窗
   * 2.点击【积分商城】进入积分详情页面时
   * 3.会员授权成功后
   * 4.点击【会员中心】按钮
   */
  getSubscribeMessage: function () {
    memberModel.subscribeMessage({
      merchantId: wx.getStorageSync('merchantId'),
      position: 1
    }).then(data => {
      if (data && data.length > 0) {
        let tmplId = []
        data.forEach(element => {
          tmplId.push(element.templateId)
        })
        wx.requestSubscribeMessage({
          tmplIds: tmplId,
          success(res) {},
          fail(err) {}
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.currency.title,
      imageUrl: 'https://oss.kiscloud.net/wechat/images/detail-share.png',
      path: `pages/currencyShop/currencyDetail/index?shopName=${this.data.shopName}&currency=${encodeURIComponent(JSON.stringify(this.data.currency))}&shopId=${this.data.shopId}&type=${this.data.type}`
    }
  }
})